package com.fly.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fly.common.R;
import com.fly.pojo.*;
import com.fly.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/index")
public class FrBolgController {
    @Autowired
    private BlogService blogService;
    @Autowired
    private UserService userService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;
    @Autowired
    private TagUserService tagUserService;
    @GetMapping("/Blogs")
    public R<Page> getPage(Integer page, int pageSize){
        System.out.println("asd");
        Page<Blog> pageInfo =new Page<>(page,pageSize);
        Page<BlogDto> blogDtoPage =new Page<>();
        LambdaQueryWrapper<Blog> queryWrapper=new LambdaQueryWrapper<Blog>();
        queryWrapper.orderByDesc(Blog::getUpdateTime);
        blogService.page(pageInfo,queryWrapper);
        BeanUtils.copyProperties(pageInfo,blogDtoPage,"records");
        List<Blog> records= pageInfo.getRecords();
        List<BlogDto> BlogDto1 =  records.stream().map((item)->{
            BlogDto blogDto=new BlogDto();
            BeanUtils.copyProperties(item,blogDto);
            blogDto.setAvatar(userService.getById(blogDto.getUserId()).getAvatar());
            blogDto.setName(userService.getById(blogDto.getUserId()).getUsername());
            blogDto.setType(typeService.getById(blogDto.getTypeId()).getName());
            return blogDto;
        }).collect(Collectors.toList());
        blogDtoPage.setRecords(BlogDto1);
        return R.success( blogDtoPage);
    }

    @GetMapping("/Types")
    public R<Page> getType(Integer page, int pageSize){

        Page<Type> pageInfo =new Page<>(page,pageSize);
        Page<TypeCount> typeCountPage =new Page<>();
        typeService.page(pageInfo);
        BeanUtils.copyProperties(pageInfo,typeCountPage,"records");
        List<Type> records=pageInfo.getRecords();
       List <TypeCount> typeCounts = records.stream().map((item)->{
            TypeCount typeCount = new TypeCount();
            BeanUtils.copyProperties(item,typeCount);
            LambdaQueryWrapper<Blog> queryWrapper=new LambdaQueryWrapper<Blog>();
            queryWrapper.eq(Blog::getTypeId, typeCount.getId());


            typeCount.setCount(  blogService.count(queryWrapper));
            return typeCount;
        }).collect(Collectors.toList());
       typeCountPage.setRecords(typeCounts);
        return R.success(  typeCountPage);
    }

    @GetMapping("/Tags")
    public R<Page> getTags(Integer page, int pageSize){

        Page<Tag> pageInfo =new Page<>(page,pageSize);
        Page<TagDto> dtoPage =new Page<>();
        tagService.page(pageInfo);
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");
        List<Tag> records=pageInfo.getRecords();
        List<TagDto> tagDtos =  records.stream().map((item)->{
            TagDto tagDto =new TagDto();
            BeanUtils.copyProperties(item,tagDto);
            LambdaQueryWrapper<TagAndUerID> queryWrapper=new LambdaQueryWrapper<TagAndUerID>();
            queryWrapper.eq(TagAndUerID::getTagsId,tagDto.getId());

            tagDto.setCount(tagUserService.count(queryWrapper));
            return tagDto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(tagDtos);
        return R.success(  dtoPage);
    }
}
